package com.yibai.ssodemo.controller;


import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
public class IndexController {




    @RequestMapping("/")
    public ModelAndView index() {

        if(StpUtil.isLogin()){
            Map<String, Object> map = new HashMap<>();
            map.put("loginId", StpUtil.getLoginId());

            map.put("userName",StpUtil.getAnonTokenSession().get("userName"));
            map.put("nickName",StpUtil.getAnonTokenSession().get("nickName"));
            map.put("phonenumber",StpUtil.getAnonTokenSession().get("phonenumber"));
            map.put("avatar",StpUtil.getAnonTokenSession().get("avatar"));

            return new ModelAndView("index.html",map);
        }else{
            return new ModelAndView("login.html");
        }
    }


    @RequestMapping("/callback")
    public ModelAndView callback() {
        return new ModelAndView("callback.html");
    }


    @RequestMapping("/logout")
    public void logout(HttpServletResponse response) {

        try{
            StpUtil.logout();

            /**
             * 这里也可以调用 SSO一体化平台 退出登录接口 （以实现全局退出）
             */
        }catch (Exception e){}

        try{
            //重定向到 主页
            response.sendRedirect("/");
        }catch (Exception e){}

    }



}
